/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef RC5_H
#define RC5_H 

#include <avr/io.h>
#include <avr/interrupt.h>
//#include <avr/signal.h>
#include <avr/pgmspace.h>
#include <stdlib.h>

typedef unsigned char uchar;
typedef unsigned int uint;

#define xRC5_IN 	PIND
#define xRC5		PD7
#define RC5TIME 	1.778e-3		// 1.778msec
#define PULSE_MIN	(uchar)(XTAL / 512 * RC5TIME * 0.4 + 0.5)
#define PULSE_1_2	(uchar)(XTAL / 512 * RC5TIME * 0.8 + 0.5)
#define PULSE_MAX	(uchar)(XTAL / 512 * RC5TIME * 1.2 + 0.5)
#define XTAL		16e6
#define xSEND_LED	PD6
#define xSEND_OUT 	PORTD


extern uchar	rc5_bit;				// bit value
extern uchar	rc5_time;				// count bit time
extern uint	rc5_tmp;				// shift bits in
extern uint	rc5_data;				// store result

/** 
 * \brief This struct represents a received/sendable rc5-struct.
 */
typedef struct rc5_t {
	uint flip;
	uint addr;
	uint value;
} rc5;

/** 
 * \brief read_rc5 reads rc5-data from the TSOP-36xx Receiver. If no data was
 * received the flip part of the structure is set to 100
 * 
 * \return a rc5-object is returned.
 */
rc5 read_rc5();
/** 
 * \brief send_rc5 sends rc5 data. This call is blocking and it lasts about
 * 100ms.
 * 
 * \param val is the struct which should be sent.
 */
void send_rc5(rc5 val);

#endif
